package com.beiji.lib.pen.utils;

/**
 * 检查蓝牙设备是否是腾千里笔（PenNote移植代码）
 * Created by X on 2018/7/11.
 */
public class PenFilterUtil {

    private final static byte[] hex = "0123456789ABCDEF".getBytes();

    private static String Bytes2HexString(byte[] b) {
        byte[] buff = new byte[2 * b.length];
        for (int i = 0; i < b.length; i++) {
            buff[2 * i] = hex[(b[i] >> 4) & 0x0f];
            buff[2 * i + 1] = hex[b[i] & 0x0f];
        }
        return new String(buff);
    }

    public static boolean isSmartPen(byte[] scanRecord) {
//        String retBefore = "";
//        for (byte item :
//                scanRecord) {
//            retBefore += item;
//        }
//        Log.i(TAG, "before ret = " + retBefore);
//        String ret = Bytes2HexString(scanRecord);
//        Log.i(TAG, "ret = " + ret);
        int index = 0;
        int len = 0;
        int val = 0;

        //02 01 05
        //0B 09 53 6D 61 72 74 50 65 6E 37 42
        //11 FF 313233347B0889ECF1B000FFF7540365

        index = scanRecord[index] & 0xff;   //长度 2
        len = index + 1;                    //3
        index = scanRecord[len] & 0xff;     //得到第二行的长度
        len = len + index + 1;              //得到第三行第一个字节的下标
        val = scanRecord[len] & 0xff;       //得到第三行的长度

        if (val == 17) {
            String stmp = Integer.toHexString(scanRecord[len + 1] & 0xFF);
            if (("ff").equalsIgnoreCase(stmp)) {
                byte[] b = new byte[4];
                b[0] = scanRecord[len + 2];
                b[1] = scanRecord[len + 3];
                b[2] = scanRecord[len + 4];
                b[3] = scanRecord[len + 5];
                stmp = Bytes2HexString(b);
                return "31323334".equalsIgnoreCase(stmp);
            }
        }
        return false;
    }
}
